<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="GRID_PAGE_SIZE" />
<atleap:globalProperty name="${pageScope.GRID_PAGE_SIZE}" defaultValue="10" type="integer" var="pageSize" />

<tiles:importAttribute />

<c:set var="infoCells" value="${infoCells}" scope="request" />
<c:set var="constantButtonCells" value="0" scope="request" />
<c:set var="additionalCells" value="0" scope="request" />

<c:set var="maxBarWidth" value="100" scope="request" />
<c:set var="barHeight" value="10" scope="request" />

<atleap:content identifier="information" ignore="true" />

<atleap:message key="core.commons.buttons.select" var="selectMsg" />
<atleap:form action="${listAction}" method="GET" validateOnSubmit="false">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td valign="middle">
                <strong>
                    <atleap:message key="core.statistics.top.form.from" />
                </strong>
                &nbsp;
            </td>
            <td valign="middle">
                <html:text property="date1" styleClass="date" readonly="true" />
                <atleap:calendar fieldName="document.forms['twoDatesForm'].date1" showYearNavigation="true" showYearNavigationInput="true" >
                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/calendar.gif" />" border="0" height="20" width="20" alt="${selectMsg}" title="${selectMsg}" />
                </atleap:calendar>
            </td>
            <td valign="middle">
                &nbsp;
                <strong>
                    <atleap:message key="core.statistics.top.form.till" />
                </strong>
                &nbsp;
            </td>
            <td valign="middle">
                <html:text property="date2" styleClass="date" readonly="true" />
                <atleap:calendar fieldName="document.forms['twoDatesForm'].date2" showYearNavigation="true" showYearNavigationInput="true" >
                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/calendar.gif" />" border="0" height="20" width="20" alt="${selectMsg}" title="${selectMsg}" />
                </atleap:calendar>
            </td>
            <td valign="middle">
                &nbsp;
                <atleap:submit styleClass="button" style="margin-top: 0px;">
                    <atleap:message key="core.commons.buttons.refresh" />
                </atleap:submit>
            </td>
        </tr>
    </table>
</atleap:form>

<atleap:grid name="${gridName}" pageSize="${pageSize}">
    <atleap:queryParameter name="fromDate" value="${requestScope.fromDate}" />
    <atleap:queryParameter name="tillDate" value="${requestScope.tillDate}" />
    <atleap:fetchRows beanId="statisticsManager" method="${methodName}" var="objects" />

    <%-- Calculating max values --%>
    <c:set var="maxTotal" value="1" />
    <atleap:iterator items="${objects}" var="object" >
        <c:set var="total" value="${object.totalCount}" />
        <c:if test="${total > maxTotal}">
            <c:set var="maxTotal" value="${total}" />
        </c:if>
    </atleap:iterator>

    <table border="0" cellpadding="0" cellspacing="0" class="grid" id="${gridId}">
        <thead>
            <tiles:insert name="headFragment" />
            <th nowrap="nowrap" width="40%">
            </th>
            <th nowrap="nowrap">
                <atleap:message key="core.statistics.visit.form.number" />
            </th>
            <c:if test="${constantButtonCells + additionalCells > 0}">
                <th colspan="${constantButtonCells + additionalCells}">
                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/spacer.gif" />" border="0"  />
                </th>
            </c:if>
            </thead>
        <tbody>
            <atleap:rowsIterator collection="${objects}" var="info" oddClass="oddRow" evenClass="evenRow">
                <c:set var="object" value="${info.object}" scope="request" />
                <c:set var="total" value="${info.totalCount}" />
                <c:set var="totalFraction" value="${total / maxTotal}" />
                <fmt:formatNumber value="${totalFraction * maxBarWidth}" maxFractionDigits="0" groupingUsed="false" var="totalBarWidth" />
                <tr class="${rowClass}">
                    <tiles:insert name="bodyFragment" />
                    <td class="defaultCursorCell">
                        <table border="0" cellpadding="0" cellspacing="0" class="twoRowTable">
                            <tr>
                                <td class="twoRowTableCell">
                                    <img border="0" src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/statistics/color1_pixel.gif" />" width="${totalBarWidth}%" height="${barHeight}" />
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td class="defaultCursorCell" align="center">
                        <table border="0" cellpadding="0" cellspacing="0" class="twoRowTable">
                            <tr>
                                <td align="center" class="twoRowTableCell">
                                    ${total}
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </atleap:rowsIterator>
            <atleap:noRows>
                <tr>
                    <td class="tfoot" colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                       <em><atleap:message key="core.commons.nothingToShow"/></em>
                    </td>
                </tr>
            </atleap:noRows>
        </tbody>
        <tfoot>
            <tr>
                <atleap:pager pageCount="10">
                    <td colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                        <atleap:message key="core.grid.pager.title" />&nbsp;
                        <atleap:firstPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/first.gif" />" border="0" alt="<atleap:message key="core.grid.pager.first"/>" title="<atleap:message key="core.grid.pager.first"/>" /></atleap:firstPage>
                        <atleap:prevPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/prev.gif" />" border="0" alt="<atleap:message key="core.grid.pager.prev"/>" title="<atleap:message key="core.grid.pager.prev"/>" /></atleap:prevPage>
                        <atleap:pagesIterator>
                            <atleap:page>${pageNumber}&nbsp;</atleap:page>
                            <atleap:currentPage><b>${pageNumber}</b>&nbsp;</atleap:currentPage>
                        </atleap:pagesIterator>
                        <atleap:nextPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/next.gif" />" border="0" alt="<atleap:message key="core.grid.pager.next"/>" title="<atleap:message key="core.grid.pager.next"/>" /></atleap:nextPage>
                        <atleap:lastPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['adminCssTheme']}/last.gif" />" border="0" alt="<atleap:message key="core.grid.pager.last"/>" title="<atleap:message key="core.grid.pager.last"/>" /></atleap:lastPage>
                    </td>
                </atleap:pager>
            </tr>
        </tfoot>
    </table>
</atleap:grid>

<atleap:form action="core/statistics/showPanel" method="GET" validateOnSubmit="false">
    <atleap:submit styleClass="button"><atleap:message key="core.commons.buttons.back" /></atleap:submit>
</atleap:form>
